#include "NetServer.h"

using namespace Hiranipra;
using namespace Hiranipra::Net;
//using namespace Hiranipra::Platform;

NetServer::NetServer()
{
}

NetServer::~NetServer()
{
}

bool NetServer::Listen( const Address& address, size_t peerCount )
{
	_boundAddress = address;
	_selfHost = enet_host_create( address.GetAddress(), peerCount, 0, 0 );
	if( _selfHost == NULL )
		return false;

	//

	return true;
}

bool NetServer::Broadcast( uint8 channelId, Packet* packet )
{
	enet_host_broadcast( _selfHost, channelId, packet->GetInternalPacket() );
	return true;
}

void NetServer::OnConnect( ENetEvent& ev )
{
	Peer* peer = new Peer( ev.peer );
	ev.peer->data = peer;
}

void NetServer::OnDisconnect( ENetEvent& ev )
{
	Peer* peer = ( ev.peer != NULL ) ? ( Peer* )ev.peer->data : NULL;
	if( peer != NULL )
	{
		peer->Release();
		ev.peer->data = NULL;
	}
}
